const fs = require('fs');
const path = require('path');

const subDirectorys = ["DBServer", "LoginGate", "LoginSrv", "Mir200", "RunGate", "SelGate"]
async function findDirectory(rootPath) {
    try {
        const files = await fs.promises.readdir(rootPath, { withFileTypes: true });
        const names = files.map(v => v.name);
        if (subDirectorys.every(v => names.indexOf(v) !== -1)) {
            return rootPath
        }
        for (const file of files) {
            if (!file.isDirectory() || file.name.startsWith('.')) {
                continue;
            }
            const result = await findDirectory(path.join(rootPath, file.name))
            if (result) {
                return result;
            }
        }
        return null;
    } catch (err) {
        console.error('读取目录时出错:', err);
        throw err;
    }
}

function sleep_ms(delay = 0) {
    return new Promise((resolve) => setTimeout(resolve, delay));
}

module.exports = {
    findDirectory, sleep_ms
}